package com.chenyh.backend;

import java.io.BufferedReader;
import java.io.Console;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Date;
import java.util.Iterator;
import java.util.List;

import org.hibernate.Session;

import com.chenyh.backend.domain.Event;
import com.chenyh.backend.domain.Person;
import com.chenyh.backend.util.HibernateUtil;

public class EventManager {

    public static void main(String[] args) {
        EventManager mgr = new EventManager();
        //BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        //String cmd = "store";
        String cmd = "list";

        if (cmd.equals("store")) {
            //mgr.createAndStoreEvent("My Event", new Date());
            //mgr.createAndStorePerson("chen", "yuheng", 20);
            //mgr.createAndStorePerson("alice", "li", 20);
        	mgr.addPersonToEvent(1L, 1L);
        	mgr.addPersonToEvent(1L, 2L);
        	mgr.addPersonToEvent(1L, 2L);
        	mgr.addPersonToEvent(2L, 2L);
        }else if (cmd.equals("list")) {
            List events = mgr.listEvents();
            for (int i = 0; i < events.size(); i++) {
                Event theEvent = (Event) events.get(i);
                System.out.println(
                        "Event: " + theEvent.getTitle() + " Time: " + theEvent.getDate()
                );
                mgr.printPerson(1L);
            }
        }

        HibernateUtil.getSessionFactory().close();
    }
    
    private List listEvents() {
        Session session = HibernateUtil.getSessionFactory().getCurrentSession();
        session.beginTransaction();
        List result = session.createQuery("from Event").list();
        session.getTransaction().commit();
        return result;
    }

    private void createAndStoreEvent(String title, Date theDate) {
        Session session = HibernateUtil.getSessionFactory().getCurrentSession();
        session.beginTransaction();

        Event theEvent = new Event();
        theEvent.setTitle(title);
        theEvent.setDate(theDate);
        session.save(theEvent);

        session.getTransaction().commit();
    }
    
    
    
    private void createAndStorePerson(String fn, String ln, int age) {
        Session session = HibernateUtil.getSessionFactory().getCurrentSession();
        session.beginTransaction();

        Person p = new Person();
        p.setAge(age);
        p.setFirstname(fn);
        p.setLastname(ln);
        session.save(p);
        
        session.getTransaction().commit();
    }
    
    private void printPerson(Long personId){
    	Session session = HibernateUtil.getSessionFactory().getCurrentSession();
    	session.beginTransaction();
    	
    	Person aPerson = (Person) session.load(Person.class, personId);
    	System.out.print("PERSON: "+aPerson.getFirstname()+", ");
    	for(Object o : aPerson.getEvents()){
    		Event e = (Event)o;
    		System.out.print(e.getTitle()+"@"+e.getDate()+',');
    	}
    	System.out.println();
    	
    	session.getTransaction().commit();
    }
    
    private void addPersonToEvent(Long personId, Long eventId) {
        Session session = HibernateUtil.getSessionFactory().getCurrentSession();
        session.beginTransaction();

        Person aPerson = (Person) session.load(Person.class, personId);
        Event anEvent = (Event) session.load(Event.class, eventId);
        aPerson.getEvents().add(anEvent);

        session.getTransaction().commit();
    }

}